package cn.lili.modules.distribution.mapper;

import cn.lili.modules.distribution.entity.dos.DistributionGoods;
import cn.lili.modules.distribution.entity.dto.DistributionGoodsSearchParams;
import cn.lili.modules.distribution.entity.vos.DistributionGoodsVO;
import cn.lili.modules.goods.entity.dos.Goods;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 分销商品数据处理层
 *
 * @author pikachu
 * @since 2020-03-24 23:04:56
 */
public interface DistributionGoodsMapper extends BaseMapper<DistributionGoods> {

    /**
     * 获取分销员未选择商品VO分页
     *
     * @param page           分页
     * @param queryWrapper   查询条件
     * @param distributionId 分销员ID
     * @return 分销员未选择商品VO分页
     */
    @Select("SELECT dg.* FROM li_distribution_goods dg WHERE dg.id NOT IN(SELECT distribution_goods_id FROM li_distribution_selected_goods WHERE distribution_id=${distributionId}) ${ew.customSqlSegment}")
    Page<DistributionGoodsVO> notSelectGoods(Page<DistributionGoodsVO> page, @Param(Constants.WRAPPER) Wrapper<DistributionGoodsVO> queryWrapper, String distributionId);

    /**
     * 获取分销员已选择分销商品VO分页
     *
     * @param page           分页
     * @param queryWrapper   查询条件
     * @param distributionId 分销员ID
     * @return 分销员已选择分销商品VO分页
     */
    @Select("SELECT dg.* FROM li_distribution_goods dg WHERE dg.id IN(SELECT distribution_goods_id FROM li_distribution_selected_goods WHERE distribution_id=${distributionId}) ${ew.customSqlSegment}")
    Page<DistributionGoodsVO> selectGoods(Page<DistributionGoodsVO> page, @Param(Constants.WRAPPER) Wrapper<DistributionGoodsVO> queryWrapper, String distributionId);

    /**
     * 获取分销商品VO分页
     *
     * @param page         分页
     * @param queryWrapper 查询条件
     * @return 分销商品VO分页
     */
    @Select("SELECT dg.* FROM li_distribution_goods dg ${ew.customSqlSegment}")
    Page<DistributionGoodsVO> getDistributionGoodsVO(Page<DistributionGoodsVO> page, @Param(Constants.WRAPPER) Wrapper<DistributionGoodsVO> queryWrapper);

    /**
     * 查找分类信息
     * @param distributionId
     * @return
     */
    @Select("SELECT g.id,g.goods_name,g.category_path,g.category_path_son FROM lilishop_distribution.li_distribution_goods AS dg " +
            " JOIN lilishop_distribution.li_distribution_selected_goods AS dsg ON dg.id = dsg.distribution_goods_id " +
            " JOIN lilishop_goods.li_goods AS g ON dg.goods_id = g.id " +
            " WHERE dsg.distribution_id = #{distributionId}")
    List<Goods> selectDistributionGoodCategoryInfo(@Param("distributionId") String distributionId);


    /**
     * 根据分类id查询分销商品信息
     * @param page
     * @param param
     * @return
     */
    @Select("SELECT dg.*\n" +
            "FROM lilishop_distribution.li_distribution_goods AS dg\n" +
            "JOIN lilishop_goods.li_goods AS g ON dg.goods_id = g.id\n" +
            "WHERE g.category_path LIKE concat(#{param.categoryId},',%')\n" +
            "AND dg.id in (SELECT distribution_goods_id FROM lilishop_distribution.li_distribution_selected_goods \n" +
            "WHERE distribution_id = #{param.distributionId})")
    Page<DistributionGoodsVO> getDistributionGoodListByOpenIdAndCategoryInfo(Page<DistributionGoodsVO> page, @Param("param") DistributionGoodsSearchParams param);
}